import os
import yaml


class ReadYaml:
    #读取yaml文件
    def __init__(self,yaml_name):
        with open(r'D:\Users\learnapitest\yaml_pytest\data\\' + yaml_name,mode='r',encoding='utf-8') as f:
            self.value = yaml.safe_load(f)

    #读取gloabl_token.yaml文件,stream文件流,Loader加载方式
    # def read_gloabl_token(self):
    #     with open(r'D:\Users\learnapitest\pytest-data-json-report\gloabl_token.yaml',mode='r',encoding='utf-8') as f:
    #         value = yaml.load(stream=f,Loader=yaml.FullLoader)
    #         return value

    #写入gloable_token.yaml文件
    def write_gloabl_token(self,data):
        with open(r'D:\Users\learnapitest\yaml_pytest\data\gloabl_token.yaml', mode='w',encoding='utf-8') as f:
            yaml.dump(data=data,stream=f,allow_unicode=True )

    #清楚gloable_token.yaml文件
    def clear_gloable_yaml(self):
        with open(r'D:\Users\learnapitest\yaml_pytest\data\gloabl_token.yaml',mode='w',encoding='utf-8') as f:
            f.truncate()



    def get_yamldata(self,key):
        return self.value[key]



if __name__ == '__main__':
    ya = ReadYaml('login_data.yaml')
    print(ya.get_yamldata("test_login1"))